<template>
  <div>
    <a-card :bordered="false" class="title-main">
      <a-row :gutter="[0,16]" class="title-it">
        <a-col :span="6">
          業主姓名：{{ leaseData.name}}
        </a-col>
        <a-col :span="6">
          聯係電話：{{ leaseData.phone}}
        </a-col>
        <a-col :span="12">
          <div>
          <a-button type="primary" ghost class="hhBum" @click="$refs.editOwnerDetailForm.edit(leaseId)">
            業主詳情
          </a-button>
          <a-button  @click="$refs.billPlan.edit(leaseId)" v-if="leaseData.leaseStatus !== 2" type="primary" ghost class="hhBum">
            賬單計劃
          </a-button>
          <a-button @click="$refs.xuzuOwnerForm.edit(leaseId)" v-if="leaseData.leaseStatus !== 2 " type="primary" ghost class="hhBum">
              續租
          </a-button>
          <a-button v-if="leaseData.leaseStatus === 1" @click="$refs.tuiOwnerForm.edit(leaseId)" type="primary" ghost class="hhBum">
            退租
          </a-button>
        </div>
        </a-col>
      </a-row>
      <a-row :gutter="[0,16]" class="title-it">
        <a-col :span="6">
          租金：{{ leaseData.rent}}元
        </a-col>
        <a-col :span="6">
          收租週期：{{ leaseData.receiptCycle}}
        </a-col>
        <a-col :span="6">
          租期：{{ leaseData.rentStartDate}}~{{ leaseData.rentEndDate}}
        </a-col>
      </a-row>
      <a-row :gutter="[0,16]" class="title-it">
        <a-col :span="24">簽約房間：{{ leaseData.roomName }}</a-col>
      </a-row>
    </a-card>
     <a-card :bordered="false">
      <s-table
        ref="table"
        size="default"
        :columns="columns"
        :data="loadData"
        :extraTool="[]"
        :scroll="{ x: 1500 }"
        bordered
        :rowKey="(record) => record.id">
        <template slot="qsNum" slot-scope="text,record">
          <span v-if="record.billStatus ===3 || record.billStatus ===4">清算賬單</span>
          <span v-else>{{ text }}</span>
        </template>
        <template slot="rentStartDate" slot-scope="text,record">
          {{ text }}~{{ record.rentEndDate }}
        </template>
        <template slot="rentAmountName" slot-scope="text,record">
        <a-row v-for="(item,index) in record.details" :key="index" class="aaa">
            <a-col :span="24" class="aa"><span>{{ item.rentAmountName }}</span></a-col>
          </a-row>
        </template>
        <template slot="amount" slot-scope="text,record">
          <a-row v-for="(item,index) in record.details" :key="index" class="aaa">
            <a-col :span="24" :class="item.amount < 0 ? 'abvv': 'ab'" class="aa"><span>{{ item.amount }}</span></a-col>
          </a-row>
        </template>
        <template slot="subtotal" slot-scope="text">
         <span  :class="text < 0 ? 'abvv': 'ab' ">{{ text }}</span>
        </template>
        <template slot="receiveAmount" slot-scope="text">
         <span  :class="text < 0 ? 'abvv': 'ab' ">{{ text }}</span>
        </template>
        <template slot="collectionType" slot-scope="text">
          <span>{{ statusFkfsFilter(text) }}</span>
        </template>
         <template slot="billStatus" slot-scope="text">
          <span v-if="text === 0">
             <a-tag color="orange">
              {{ statusFilter(text) }}
            </a-tag>
          </span>
          <span v-else-if="text === 1">
             <a-tag color="green">
              {{ statusFilter(text) }}
            </a-tag>
          </span>
          <span  v-else-if="text === 3">
             <a-tag color="purple">
              {{ statusFilter(text) }}
            </a-tag>
          </span>
          <span  v-else>
             <a-tag color="cyan">
              {{ statusFilter(text) }}
            </a-tag>
          </span>
        </template>
        <span slot="operation" slot-scope="text, record">
          <a @click="$refs.readOnlyForm.edit(record.id)">查看</a>
          <a-divider type="vertical" />
          <a-dropdown >
              <a class="ant-dropdown-link">
                更多 <a-icon type="down" />
              </a>
              <a-menu slot="overlay">
                <a-menu-item v-if="record.billStatus ===1">
                  <a @click="$refs.arriveOwnerForm.edit(record.id, record.ownerLeaseId)">交租</a>
                </a-menu-item>
                <a-menu-item v-if="record.billStatus ===1">
                  <a @click="$refs.editOwnerFormOne.edit(record.id, record.remark, 1)">修改</a>
                </a-menu-item>
                <a-menu-item v-if="record.billStatus ===3">
                  <a @click="$refs.editTzqsOwnerFormOne.edit(record.id, record.remark, 2)">修改</a>
                </a-menu-item>
                <a-menu-item v-if="record.billStatus ===2">
                  <a-popconfirm  placement="topRight" title="撤銷到賬將刪除此賬單到賬時產生的流水？" @confirm="() => chexiaoDaoZhang(record.id)">
                    <a>撤銷交租</a>
                  </a-popconfirm>
                </a-menu-item>
                 <a-menu-item v-if="record.billStatus ===3">
                  <a @click="$refs.arriveQsOwnerForm.edit(record.id, record.ownerLeaseId)">退租清算</a>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
        </span>
      </s-table>
    </a-card>
    <read-only-form ref="readOnlyForm"/>
    <arrive-owner-form ref="arriveOwnerForm" @ok="handleOk" />
    <edit-owner-form-one ref="editOwnerFormOne" @ok="handleOk" />
    <xuzu-owner-form ref="xuzuOwnerForm" @ok="handleOk" />
    <tui-owner-form ref="tuiOwnerForm" @ok="handleOk" />
    <edit-tzqs-owner-form-one ref="editTzqsOwnerFormOne" @ok="handleOk" />
    <arrive-qs-owner-form ref="arriveQsOwnerForm" @ok="handleOk" />
    <edit-owner-detail-form ref="editOwnerDetailForm" @ok="handleOk" />
    <bill-plan ref="billPlan" @ok="handleOk"/>
  </div>
</template>

<script>
  import { STable } from '@/components'
  import { ownerLeaseBillPageByLeaseId, ownerLeaseBillCxdzEdit } from '@/api/modular/main/ownerleasebill/ownerLeaseBillManage'
  import { detailLease } from '@/api/modular/main/ownerlease/ownerLeaseManage'
  import { busMeansOfTransactionList } from '@/api/modular/main/busmeansoftransaction/busMeansOfTransactionManage'
  import readOnlyForm from './readOnlyForm.vue'
  import arriveOwnerForm from './arriveOwnerForm.vue'
  import editOwnerFormOne from './editOwnerFormOne.vue'
  import xuzuOwnerForm from './xuzuOwnerForm.vue'
  import tuiOwnerForm from './tuiOwnerForm.vue'
  import editTzqsOwnerFormOne from './editTzqsOwnerFormOne.vue'
  import arriveQsOwnerForm from './arriveQsOwnerForm.vue'
  import editOwnerDetailForm from '../ownerlease/editForm.vue'
  import billPlan from './billPlan.vue'
  export default {
    components: {
      STable,
      readOnlyForm,
      arriveOwnerForm,
      editOwnerFormOne,
      xuzuOwnerForm,
      tuiOwnerForm,
      editTzqsOwnerFormOne,
      arriveQsOwnerForm,
      editOwnerDetailForm,
      billPlan
    },
    data () {
      return {
        // 表头
        columns: [
          {
            title: '期數',
            align: 'center',
            dataIndex: 'qsNum',
            scopedSlots: { customRender: 'qsNum' }
          },
          {
            title: '收租日',
            align: 'center',
            dataIndex: 'rentedDay'
          },
          {
            title: '賬單週期',
            align: 'center',
            dataIndex: 'rentStartDate',
            scopedSlots: { customRender: 'rentStartDate' }
          },
          {
            title: '類別',
            align: 'center',
            dataIndex: 'rentAmountName',
            scopedSlots: { customRender: 'rentAmountName' }
          },
          {
            title: '金额',
            align: 'center',
            dataIndex: 'amount',
            scopedSlots: { customRender: 'amount' }
          },
          {
            title: '小計',
            align: 'center',
            dataIndex: 'subtotal',
            scopedSlots: { customRender: 'subtotal' }
          },
          {
            title: '實收金額',
            align: 'center',
            dataIndex: 'receiveAmount',
            scopedSlots: { customRender: 'receiveAmount' }
          },
          {
            title: '實收日期',
            align: 'center',
            dataIndex: 'receiveDate'
          },
          {
            title: '收款方式',
            align: 'center',
            dataIndex: 'collectionType',
            scopedSlots: { customRender: 'collectionType' }
          },
          {
            title: '備註',
            align: 'center',
            dataIndex: 'remark'
          },
          {
            title: '到账状态',
            align: 'center',
            dataIndex: 'billStatus',
            scopedSlots: { customRender: 'billStatus' }
          },
          {
            title: '操作',
            key: 'action',
            width: 150,
            scopedSlots: { customRender: 'operation' }
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return ownerLeaseBillPageByLeaseId(Object.assign(parameter, { ownerLeaseId: this.leaseId })).then((res) => {
            return res.data
          })
        },
        collectionTypeData: [],
        billStatusDictTypeDropDown: [ { code: 1, value: '未交租' }, { code: 2, value: '已交租' }, { code: 3, value: '清算狀態' }, { code: 4, value: '清算到賬' } ],
        leaseId: '',
        leaseData: {}
      }
    },
    created () {
     this.leaseId = this.$route.query.id
     this.initNeedData()
    },
    methods: {
      initNeedData() {
        this.getOwnerBillDetail()
        busMeansOfTransactionList().then((res) => {
          if (res.code === 200) {
            this.collectionTypeData = res.data
          } else {
            this.$message.error(res.msg)
          }
        })
      },
      getOwnerBillDetail() {
          detailLease(this.leaseId).then((res) => {
          if (res.code === 200) {
            this.leaseData = res.data
          } else {
            this.$message.error(res.msg)
          }
        })
      },
      statusFkfsFilter (id) {
        // eslint-disable-next-line eqeqeq
        const values = this.collectionTypeData.filter(item => item.id == id)
        if (values.length > 0) {
          return values[0].name
        }
      },
      statusFilter (sex) {
        // eslint-disable-next-line eqeqeq
        const values = this.billStatusDictTypeDropDown.filter(item => item.code == sex)
        if (values.length > 0) {
          return values[0].value
        }
      },
      chexiaoDaoZhang(id) {
        ownerLeaseBillCxdzEdit({ id: id }).then((res) => {
          if (res.code === 200) {
            this.handleOk()
          } else {
            this.$message.error(res.msg)//  + res.message
          }
        })
      },
      handleOk() {
        this.getOwnerBillDetail()
        this.$refs.table.refresh()
      }
    }
  }
</script>
<style  lang="less" scoped>
  .title-main{
    .ant-card-body{
      padding: 0px 24px;
    }
  }
  .qyfj{
    color: rgba(0, 0, 0, 0.85);
    font-weight: 500;
    margin-right: 6px;
  }
  .title-it{
    background-color: rgb(243, 241, 241);
    padding-left: 10px;
    font-size: 16px;
  }
  .aaa{
    border-bottom: 1px solid #ccc;
    margin-bottom: 6px;
  }
  .aa{
    text-align: center;
    font-size: 16px;
  }
  .ab{
    color: red;
  }
  .abvv{
    color: green;
  }
  .hhBum{
    margin-right: 10px;
    float: right;
  }
</style>
